import com.mysql.cj.jdbc.MysqlDataSource;

import javax.sql.DataSource;
import java.sql.*;
import java.text.MessageFormat;
import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 32309
 * Date: 2025-07-05
 * Time: 17:41
 */
public class Datasource_PreparedStatement {
    public static void main(String[] args) throws SQLException {
        // 定义mysql数据源对象
        MysqlDataSource mysqlDataSource = new MysqlDataSource();

        // 设置数据库连接串URL
        mysqlDataSource.setURL("jdbc:mysql://127.0.0.1:3306/test?characterEncoding=utf8" + "&allowPublicKeyRetrieval=true&useSSL=false");

        // 用户名
        mysqlDataSource.setUser("root");

        // 密码
        mysqlDataSource.setPassword("580231");

        // 定义JDBC数据源对象
        DataSource dataSource = mysqlDataSource;

        // 连接对象
        Connection connection = null;
        // 预处理sql连接对象
        PreparedStatement preparedStatement = null;
        // 结果集对象
        ResultSet resultSet = null;

        try {
            // 1. 通过数据源获取数据库科连接
            connection = dataSource.getConnection();
            // 2. 获取预处理sql对象，定义sql
            //String sql = "insert into student(sn,name,mail,class_id) values (?,?,?,?)";
            String sql = "select student_id,sn,name,mail,class_id from student where name = ? ";
            preparedStatement = connection.prepareStatement(sql);
            // 用户输入
//            Scanner scanner = new Scanner(System.in);
//            System.out.println("请输入学号：");
//            String stu_sn = scanner.nextLine();
//            System.out.println("请输入姓名：");
//            String stu_name = scanner.nextLine();
//            System.out.println("请输入邮箱：");
//            String stu_mail = scanner.nextLine();
//            System.out.println("请输入班级编号：");
//            long classId = scanner.nextInt();

            System.out.println("请输入姓名：");
            Scanner scanner = new Scanner(System.in);
            String name = scanner.nextLine();
            // 3. 真实值替换占位符
//            preparedStatement.setString(1,stu_sn);
//            preparedStatement.setString(2,stu_name);
//            preparedStatement.setString(3,stu_mail);
//            preparedStatement.setLong(4,classId);
            preparedStatement.setString(1,name);

            // 4. 执行sql，获取结果集
            resultSet = preparedStatement.executeQuery();

            // 5. 遍历结果集
            while (resultSet.next()) {
                Long id = resultSet.getLong(1);
                String stu_sn = resultSet.getString(2);
                String stu_name = resultSet.getString(3);
                String stu_mail = resultSet.getString(4);
                long classId = resultSet.getLong(5);
                System.out.println(MessageFormat.format("学生编号 = {0}, 学号 = {1}, 姓名 = {2}, 邮箱 = {3}, 班级编号 = {4}",id,stu_sn,stu_name,stu_mail,classId));
            }


        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            // 关闭连接，释放资源
            if (resultSet != null) {
                resultSet.close();
            }
            if (preparedStatement != null) {
                preparedStatement.close();
            }
            if (connection != null) {
                connection.close();
            }
        }
    }
}
